

import {
  get,
  post,
} from "./base_service"


/**
 * 配置相关接口
 *
 * allen  2021-08-02
 */

/**
 * 登录微信获取唯一openId
 */
const loginWxAction = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: res => {
        console.log("微信登录返回:" + JSON.stringify(res));
        // wx.setStorageSync("openId", res.code);
        post('/api/front/wechat/authorize/mini/login?code=' + res.code, {
          code: res.code
        }).then((data) => {
          // console.log("JJLog:登录请求返回:" + JSON.stringify(data));
          if (data.code == 200) {
            // wx.setStorageSync("isAuth", data.data.isAuth)
            wx.setStorageSync("openId", data.data.token)
            resolve(data)
          } else {
            reject()
          }
        }).catch((err) => {
          console.log("JJLog:微信初始化失败:" + JSON.stringify(err));
          reject()
        })
      },
      fail() {

        reject()
      }
    })
  })
}




/**
 * 获取主题配置
 */
const getThemeStyleConfig = () => {
  return new Promise((resolve, reject) => {
    // console.log("JJLog:请求开始:==========")
    setTimeout(() => {
      // console.log("JJLog:请求结束:==========")
      const res = {
        code: 200,
        data: {
          //主题色
          mainColor: '#FF0000',
          mainBlcakColor: "#333333",
          mainGrayColor: "#999999",
          simpleGrayColor: "#666666",
          mainWhiteColor: "#FFFFFF",
          mainRedColor: "#FF1800",
          grayBgColor: "#EEEEEE",
          tagColor: "#25344A",
          tagBgColor: "#FFCC00",
          subColor: '#454545',
          bigFont: "32rpx",
          commonFont: "30rpx",
          normalFont: "28rpx",
          middleFont: "26rpx",
          smallFont: "24rpx",
          littleFont: "20rpx",
        }
      }
      if (res.code == 200) {
        resolve(res)

      } else {
        reject("请求出错了")
      }
    }, 2000)
  })
}

/**
 * 获取小程序全局配置
 */
const globalSetting = () => {
  return new Promise((resolve, reject) => {
    get("/setting").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}


/**
 * 获取省市区联动
 */
const cityLinkage = () => {
  return new Promise((resolve, reject) => {
    get("/api/front/city/list").then((data) => {
      if (data.code == 200) {
        resolve(data)
      } else {
        reject(data.msg)
      }
    }).catch((e) => {
      reject(e.message)
    })
  })
}

/**
 * 发送验证码请求
 * @param {*} phone 
 */
const sendApplyAgentCodeAction = (phone) => {
  return new Promise((resolve, reject) => {
    get('/sms/send', {
      phone: phone
    }).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 更新用户昵称和头像
 * @param {*} nickName 
 * @param {*} avatar 
 */
const saveNickNameAndAvatarAction = (nickName, avatar) => {
  return new Promise((resolve, reject) => {
    get('/user/update/nickname/avatar', {
      nickname: nickName,
      avatar: avatar
    }).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      })
  })
}

/**
 * 登录
 * @param {*} sms_login 
 */
const smsLoginAction = (sms_login) => {
  return new Promise((resolve, reject) => {
    post("/user/sms/login", sms_login).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}
/**
 * 微信公众号
 *  
 */
const getQrCode = () => {
  return new Promise((resolve, reject) => {
    get("/api/front/user/mp/qrCode").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}




/**
 * 微信登录
 * @param {*} 微信登录 
 */
const decryptWxPhone = (data) => {
  return new Promise((resolve, reject) => {
    post("/user/decrypt/wx/phone", data).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 查询个人中心配置
 */
const queryPersonalModule = () => {
  return new Promise((resolve, reject) => {
    get("/setting/personal").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 查询公众号二维码
 */
const queryOfficalQrCode = () => {
  return new Promise((resolve, reject) => {
    get("/qr/code/mp").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}


/**
 * 查询oa菜单列表
 */
const queryOAMenuList = () => {
  return new Promise((resolve, reject) => {
    get("/oa/menu/list").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 心跳操作
 */
const heartBeatAction = () => {
  return new Promise((resolve, reject) => {
    get("/im/heartbeat").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 消息发送后发送信息
 * @param {*} data 
 */
const sendImMsgConfig = (data) => {
  return new Promise((resolve, reject) => {
    post("/im/send/msg", data).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 获取在线客服
 */
const queryOnlineCustomer = () => {
  return new Promise((resolve, reject) => {
    get("/user/online/customer").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 提交投诉建议
 * @param {*} data 
 */
const feedBackAction = (data) => {
  return new Promise((resolve, reject) => {
    post("/suggest/create", data).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 意见投诉列表
 * @param {*} page 
 * @param {*} pageSize 
 */
const feedBackList = (page, pageSize) => {
  return new Promise((resolve, reject) => {
    get("/suggest/manager/list?page=" + page + "&pageSize=" + pageSize).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 用户更新share
 */
const userUpdateShareRequest = () => {
  return new Promise((resolve, reject) => {
    get("/user/update/share").
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 查询银行卡列表
 * @param {*} words 
 */
const queryBankList = (words) => {
  return new Promise((resolve, reject) => {
    get("/bank?words=" + words).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}


/**
 * 处理投诉意见是否有效
 * @param {*} id 
 * @param {*} isEffective 
 */
const dealSuggestIsEffective = (id, isEffective) => {
  return new Promise((resolve, reject) => {
    get("/suggest/manager/isEffective?id=" + id + "&isEffective=" + isEffective).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 根据type查询banners
 * 1:首页banner,7:开屏广告
 * @param {*} type 
 */
const queryBannersByType = (type) => {
  return new Promise((resolve, reject) => {
    get("/banner/types?type=" + type).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 获取已开放城市
 */
const queryOpenCity = (words) => {
  return new Promise((resolve, reject) => {
    get("/search/query/open/city?words=" + words).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 根据城市查询筛选条件
 * @param {*} cityName 
 */
const queryFilterConditionByCityName = (cityName) => {
  return new Promise((resolve, reject) => {
    get("/search/query/filter/condition?cityName=" + cityName).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}


/**
 * 更新城市信息
 * @param {*} cityName 
 */
const updateCityConfig = (provinceName, cityName, areaName) => {
  return new Promise((resolve, reject) => {
    get("/user/update/city?provinceName=" + provinceName + "&cityName=" + cityName + "&areaName=" + areaName).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}


/**
 * 根据openId查询个人详情
 * @param {*} openId 
 */
const queryPersonInfoByOpenId = (openId) => {
  return new Promise((resolve, reject) => {
    get("/user/get/user/info?openId=" + openId).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

//获取直播列表
const getLiveList = (data) => {
  return new Promise((resolve, reject) => {
    post('/live/list', data).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * check状态查询
 */
const queryCheckIsOpen = () => {
  return new Promise((resolve, reject) => {
    get('/check/isOpen').
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 获取图片上传签名
 */
const getFrontUploadSign = () => {
  return new Promise((resolve, reject) => {
    get('/api/front/upload/sign?model=' + 'user' + "&pid=" + 7).
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.msg)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}

/**
 * 获取积分配置
 */
const getInterConfig = () => {
  return new Promise((resolve, reject) => {
    get('/api/front/index/integral/config').
      then((data) => {
        if (data.code == 200) {
          resolve(data)
        } else {
          reject(data.message)
        }
      }).
      catch((e) => {
        reject(e.message)
      })
  })
}








module.exports = {
  loginWxAction,
  getThemeStyleConfig,
  globalSetting,
  cityLinkage,
  sendApplyAgentCodeAction,
  saveNickNameAndAvatarAction,
  smsLoginAction,
  decryptWxPhone,
  queryPersonalModule,
  queryOfficalQrCode,
  queryOAMenuList,
  heartBeatAction,
  sendImMsgConfig,
  queryOnlineCustomer,
  feedBackAction,
  feedBackList,
  userUpdateShareRequest,
  queryBankList,
  dealSuggestIsEffective,
  queryBannersByType,
  queryOpenCity,
  queryFilterConditionByCityName,
  updateCityConfig,
  queryPersonInfoByOpenId,
  getLiveList,
  queryCheckIsOpen,
  getFrontUploadSign,
  getInterConfig,//获取积分配置
  getQrCode,
}

